package com.shelpe.services.sinbad.controller.dto;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.entity.jpa.AutoProductQueue;
import com.shelpe.services.sinbad.entity.jpa.Campaign;

import lombok.Data;

@Data
public class AutoProductDto {
	private static String PRODUCT_URL = "http://img.taobaocdn.com/bao/uploaded";

	@JsonProperty("product_id")
	private long productID;
	@JsonProperty("product_name")
	private String productName;
	@JsonProperty("product_url")
	private String productUrl;
	private String message;
	private String status;
	private int progress;
	private int repeatable;
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
	@JsonProperty("create_time")
	private Date createTime;

	@JsonProperty("campaign_id")
	private long campaignID;
	@JsonProperty("campaign_name")
	private String campaignName;

	public void from(AutoProductQueue queue) {
		this.productID = queue.getProductID();
		this.productName = queue.getFirstCreativeName();
		this.createTime = queue.getCreateTime();
		this.progress = queue.getProgress();
		this.status = queue.getStatus().name();
		this.repeatable = queue.getRepeatable();
		if (StringUtils.isNotBlank(queue.getMessage())) {
			this.message = queue.getMessage();
		}
		if (!queue.getFirstCreativeUrl().contains(PRODUCT_URL)) {
			this.productUrl = PRODUCT_URL + queue.getFirstCreativeUrl();
		}
	}

	public void from(Campaign campaign) {
		this.campaignID = campaign.getAutoID();
		this.campaignName = campaign.getTitle();
	}
}
